﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using System.Web.Mvc;

namespace SRF.Common.Validation
{
    /// <summary>
    /// 数据类型验证
    /// </summary> 
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public sealed class DataTypeValidator : System.ComponentModel.DataAnnotations.ValidationAttribute, IClientValidatable
    {
        string pattern;

        public DataTypeValidator(string pattern, string errorMessage)
            : base(errorMessage)
        {
            this.pattern = pattern;
            base.ErrorMessage = errorMessage;
        }

        public DataTypeValidator(string pattern)
        {
            string errorMessage = "";
            switch (pattern)
            {
                case DataTypePattern.Url:
                    errorMessage = "手机号码格式不正确";
                    break;
                case DataTypePattern.QQ:
                    errorMessage = "QQ格式不正确";
                    break;
                case DataTypePattern.Telephone:
                    errorMessage = "固话号码格式不正确";
                    break;
                case DataTypePattern.Mobile:
                    errorMessage = "手机号码格式不正确";
                    break;
                case DataTypePattern.Phone:
                    errorMessage = "电话(手机)号码格式不正确";
                    break;
                case DataTypePattern.Zip:
                    errorMessage = "邮编格式不正确";
                    break;
                case DataTypePattern.Email:
                    errorMessage = "Email格式不正确";
                    break;
                case DataTypePattern.Date:
                    errorMessage = "日期格式不正确";
                    break;
                case DataTypePattern.Identity:
                    errorMessage = "身份证格式不正确";
                    break;
                case DataTypePattern.Money:
                    errorMessage = "输入的整小数不正确";
                    break;
                case DataTypePattern.Integer:
                    errorMessage = "输入的正整数不正确";
                    break;
                case DataTypePattern.Double:
                    errorMessage = "小数格式不正确";
                    break;
                case DataTypePattern.Digit:
                    errorMessage = "只能输入数字";
                    break;
                case DataTypePattern.Letter:
                    errorMessage = "只允许输入英文";
                    break;
                case DataTypePattern.Chinese:
                    errorMessage = "只允许输入中文";
                    break;
            }
            base.ErrorMessage = errorMessage;
            this.pattern = pattern;
        }

        public override bool IsValid(object value)
        {
            if (value != null)
            {
                return System.Text.RegularExpressions.Regex.IsMatch(value.ToString(), pattern);
            }
            else
            {
                return true;
            }
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule();
            rule.ErrorMessage = base.ErrorMessage;
            rule.ValidationParameters["pattern"] = this.pattern;
            rule.ValidationType = "regex";
            return new[] { rule };
        }
    }
}